﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

/*=====================================================================
  This file is part of a Microsoft SQL Server Shared Source Application.
  Copyright (C) Microsoft Corporation.  All rights reserved.
 
THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.
======================================================= */


namespace Microsoft.SqlCommunity.FaultRetry
{
    /// <summary>
    /// This class represents a primary server plus its mirror server.
    /// </summary>
    public class ServerSet
    {
        public string Principal { get; set; }  // The name of the primary server (at least initially!)
        public string Mirror { get; set; }  // The name of the secondary server (at least initially!)

        /// <summary>
        /// A constructor which initializes the principle and mirror server names.
        /// </summary>
        /// <param name="principalServer">The name of the primary server</param>
        /// <param name="mirrorServer">The name of the secondary server</param>
        public ServerSet(string principalServer, string mirrorServer)
        {
            Principal = principalServer;
            Mirror = mirrorServer;
        }

        /// <summary>
        /// Returns true if both objects are ServerSets and both the principal and mirror names match.
        /// </summary>
        /// <param name="obj">The object to compare this server set to.</param>
        /// <returns>True if the server sets are similar.</returns>
        public override bool Equals(object obj)
        {
            if (!(obj is ServerSet)) return false;
            ServerSet otherServerSet = (ServerSet)obj;
            if (otherServerSet == null) 
                return this == null;
            if (this == null) 
                return otherServerSet == null;

            return this.Principal.Equals(otherServerSet.Principal, StringComparison.InvariantCultureIgnoreCase) &&
                this.Mirror.Equals(otherServerSet.Mirror, StringComparison.InvariantCultureIgnoreCase);
        }

        /// <summary>
        /// Compute a decent hash for this server set by xoring the hash of the principal name
        /// with the hash of the mirror name.
        /// </summary>
        /// <returns>A hash for this server set</returns>
        public override int GetHashCode()
        {
            return Principal.GetHashCode() ^ Mirror.GetHashCode();
        }

        /// <summary>
        /// Provide a simple way to see the principal and mirror names for this server set
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return string.Format("{0},{1}", Principal, Mirror);
        }

        // TODO: Add a Parse method.

    }

    /// <summary>
    /// Represents a collection of ServerSet objects as defined above.  We use ObservableCollection so that we can
    /// effectively data bind this collection in WPF if we want.
    /// </summary>
    public class ServerSets : ObservableCollection<ServerSet>
    {
    }
}
